package bb.lanxing.lib.devices.ble.ble.base;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import bb.lanxing.lib.devices.ble.utils.UUIDExtKt;
import bb.lanxing.lib.devices.sprint.Commands;
import bb.lanxing.lib.devices.sprint.Response;
import bb.lanxing.lib.devices.utils.GattExtKt;
import bb.lanxing.lib.kt.ByteArrayExtKt;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import kotlin.Metadata;
import kotlin.Result;
import kotlin.ResultKt;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.IntrinsicsKt;
//import kotlin.coroutines.jvm.internal.boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import kotlinx.coroutines.DelayKt;
import kotlinx.coroutines.TimeoutKt;
import kotlinx.coroutines.sync.Mutex;
//import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.ParameterPacketExtension;
/* compiled from: BaseBleCharacteristic.kt */

public abstract class BaseBleCharacteristic {
    public static final Companion Companion = new Companion(null);
    private final BaseBleService bleService;
    private BluetoothGattCharacteristic rawCharacteristic;
    private Continuation<? super Response> responseContinuation;
    private Function2<? super BluetoothGatt, ? super BluetoothGattCharacteristic, Unit> valueUpdateCallback;

    public abstract void onValueUpdate(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic);

    public abstract UUID targetUUID();

    public static final int ERROR_SEND_FAIL = 2;
    public static final int ERROR_TIME_OUT = 1;
    public static final long RESPONSE_TIME_OUT = 15000;
    public final class BaseBleCharacteristic$readSynced$1 extends ContinuationImpl {
        Object L$0;
        Object L$1;
        int label;
        Object result;
        final BaseBleCharacteristic this$0;

        public BaseBleCharacteristic$readSynced$1(BaseBleCharacteristic baseBleCharacteristic, Continuation<? super BaseBleCharacteristic$readSynced$1> continuation) {
            super((Continuation<Object>) continuation);
            this.this$0 = baseBleCharacteristic;
        }

        @Override
        public Object invokeSuspend(Object obj) {
            this.result = obj;
            this.label |= Integer.MIN_VALUE;
            return this.this$0.readSynced(this);
        }
    }
    public BaseBleCharacteristic(BaseBleService bleService) {
        List<BluetoothGattCharacteristic> characteristics;
        boolean z;
//        Intrinsics.checkNotNullParameter(bleService, "bleService");
        this.bleService = bleService;
        bleService.getBleCharacteristics().add(this);
        BluetoothGattService rawService = bleService.getRawService();
        BluetoothGattCharacteristic bluetoothGattCharacteristic = null;
        if (rawService != null && (characteristics = rawService.getCharacteristics()) != null) {
            Iterator<BluetoothGattCharacteristic> it2 = characteristics.iterator();
            while (true) {
                if (!it2.hasNext()) {
                    break;
                }
                BluetoothGattCharacteristic next = it2.next();
                if (!Intrinsics.areEqual(next.getUuid(), targetUUID())) {
                    UUID uuid = next.getUuid();
//                    Intrinsics.checkNotNullExpressionValue(uuid, "it.uuid");
                    if (!Intrinsics.areEqual(UUIDExtKt.keyWord(uuid), UUIDExtKt.keyWord(targetUUID()))) {
                        z = false;
                        continue;
                        //TODO:hu
//                        if (z) {
//                            bluetoothGattCharacteristic = next;
//                            break;
//                        }
                    }
                }
                z = true;
                continue;
            }
            bluetoothGattCharacteristic = bluetoothGattCharacteristic;
        }
        this.rawCharacteristic = bluetoothGattCharacteristic;
    }

    public final BaseBleService getBleService() {
        return this.bleService;
    }

    public final BluetoothGattCharacteristic getRawCharacteristic() {
        return this.rawCharacteristic;
    }

    public final void setRawCharacteristic(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        this.rawCharacteristic = bluetoothGattCharacteristic;
    }

    public final Function2<BluetoothGatt, BluetoothGattCharacteristic, Unit> getValueUpdateCallback() {
        return (Function2<BluetoothGatt, BluetoothGattCharacteristic, Unit>) this.valueUpdateCallback;
    }

    public final void setValueUpdateCallback(Function2<? super BluetoothGatt, ? super BluetoothGattCharacteristic, Unit> function2) {
        this.valueUpdateCallback = function2;
    }

    public final Continuation<Response> getResponseContinuation() {
        return (Continuation<Response>) this.responseContinuation;
    }

    public final void setResponseContinuation(Continuation<? super Response> continuation) {
        this.responseContinuation = continuation;
    }

    public final boolean read() {
        BluetoothGattCharacteristic bluetoothGattCharacteristic = this.rawCharacteristic;
        if (bluetoothGattCharacteristic != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic2 = this.rawCharacteristic;
            String sb = getClass().getSimpleName() +
                    " do read " +
                    (bluetoothGattCharacteristic2 != null ? bluetoothGattCharacteristic2.getUuid() : null);
            log(sb);
            BluetoothGatt rawGatt = this.bleService.getRawGatt();
            if (rawGatt != null) {
                return GattExtKt.read(rawGatt, bluetoothGattCharacteristic);
            }
            return false;
        }
        return false;
    }

    public final boolean write(byte[] value) {
//        Intrinsics.checkNotNullParameter(value, "value");
        BluetoothGattCharacteristic bluetoothGattCharacteristic = this.rawCharacteristic;
        if (bluetoothGattCharacteristic != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic2 = this.rawCharacteristic;
            Boolean bool = null;
            String sb = getClass().getSimpleName() +
                    " do write " +
                    (bluetoothGattCharacteristic2 != null ? bluetoothGattCharacteristic2.getUuid() : null) +
                    " value: " +
                    ByteArrayExtKt.toHexString(value, true);
            log(sb);
            BluetoothGatt rawGatt = this.bleService.getRawGatt();
            if (rawGatt != null) {
                bluetoothGattCharacteristic.setValue(value);
                bool = GattExtKt.write(rawGatt, bluetoothGattCharacteristic);
            }
            if (bool != null) {
                return bool;
            }
        }
        return false;
    }

    public final boolean enableNotify() {
        BluetoothGattCharacteristic bluetoothGattCharacteristic = this.rawCharacteristic;
        if (bluetoothGattCharacteristic != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic2 = this.rawCharacteristic;
            String sb = getClass().getSimpleName() +
                    " do enable notify " +
                    (bluetoothGattCharacteristic2 != null ? bluetoothGattCharacteristic2.getUuid() : null);
            log(sb);
            BluetoothGatt rawGatt = this.bleService.getRawGatt();
            Boolean valueOf = rawGatt != null ? GattExtKt.enableNotification(rawGatt, bluetoothGattCharacteristic) : null;
            if (valueOf != null) {
                return valueOf;
            }
        }
        return false;
    }

    public final boolean enableIndications() {
        BluetoothGattCharacteristic bluetoothGattCharacteristic = this.rawCharacteristic;
        if (bluetoothGattCharacteristic != null) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic2 = this.rawCharacteristic;
            String sb = getClass().getSimpleName() +
                    " do enable indications " +
                    (bluetoothGattCharacteristic2 != null ? bluetoothGattCharacteristic2.getUuid() : null);
            log(sb);
            BluetoothGatt rawGatt = this.bleService.getRawGatt();
            Boolean valueOf = rawGatt != null ? GattExtKt.enableIndications(rawGatt, bluetoothGattCharacteristic) : null;
            if (valueOf != null) {
                return valueOf;
            }
        }
        return false;
    }

    public final Object delayInPeer(Continuation<? super Unit> continuation) {
        Object delay = DelayKt.delay(180L, continuation);
        return delay == IntrinsicsKt.getCOROUTINE_SUSPENDED() ? delay : Unit.INSTANCE;
    }

    public final Object readSynced(Continuation<? super Response> continuation) {
        BaseBleCharacteristic$readSynced$1 baseBleCharacteristic$readSynced$1;
        int i;
        Mutex mutex = null;
        BaseBleCharacteristic baseBleCharacteristic = null;
        Mutex mutex2;
        Throwable th;
        BluetoothGattCharacteristic bluetoothGattCharacteristic;
        Response response;
        try {
            if (continuation instanceof BaseBleCharacteristic$readSynced$1) {
                baseBleCharacteristic$readSynced$1 = (BaseBleCharacteristic$readSynced$1) continuation;
                if ((baseBleCharacteristic$readSynced$1.label & Integer.MIN_VALUE) != 0) {
                    baseBleCharacteristic$readSynced$1.label -= Integer.MIN_VALUE;
                    Object obj = baseBleCharacteristic$readSynced$1.result;
                    Object coroutine_suspended = IntrinsicsKt.getCOROUTINE_SUSPENDED();
                    i = baseBleCharacteristic$readSynced$1.label;
                    if (i != 0) {
                        ResultKt.throwOnFailure(obj);
                        mutex = this.bleService.getDevice().getMutex();
                        baseBleCharacteristic$readSynced$1.L$0 = this;
                        baseBleCharacteristic$readSynced$1.L$1 = mutex;
                        baseBleCharacteristic$readSynced$1.label = 1;
                        if (mutex.lock(null, baseBleCharacteristic$readSynced$1) == coroutine_suspended) {
                            return coroutine_suspended;
                        }
                        baseBleCharacteristic = this;
                    } else if (i != 1) {
                        if (i != 2) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        mutex2 = (Mutex) baseBleCharacteristic$readSynced$1.L$0;
                        try {
                            ResultKt.throwOnFailure(obj);
                            response = (Response) obj;
                            mutex2.unlock(null);
                            return response;
                        } catch (Throwable th2) {
                            th = th2;
                            mutex2.unlock(null);
                            throw th;
                        }
                    } else {
                        baseBleCharacteristic = (BaseBleCharacteristic) baseBleCharacteristic$readSynced$1.L$0;
                        ResultKt.throwOnFailure(obj);
                        mutex = (Mutex) baseBleCharacteristic$readSynced$1.L$1;
                    }
                    bluetoothGattCharacteristic = baseBleCharacteristic.rawCharacteristic;
                    if (bluetoothGattCharacteristic == null) {
                        BluetoothGatt rawGatt = baseBleCharacteristic.bleService.getRawGatt();
                        if (rawGatt != null) {
//                            boxing.boxBoolean(GattExtKt.read(rawGatt, bluetoothGattCharacteristic));
                        }
                        baseBleCharacteristic$readSynced$1.L$0 = mutex;
                        baseBleCharacteristic$readSynced$1.L$1 = null;
                        baseBleCharacteristic$readSynced$1.label = 2;
                        //TODO:hu
//                        Object withTimeoutOrNull = TimeoutKt.withTimeoutOrNull(/*BaseResponsiveCharacteristicKt.*/RESPONSE_TIME_OUT, new KtExt(null, baseBleCharacteristic), baseBleCharacteristic$readSynced$1);
//                        if (withTimeoutOrNull == coroutine_suspended) {
//                            return coroutine_suspended;
//                        }
                        mutex2 = mutex;
//                        obj = withTimeoutOrNull;//TODO:hu
                        response = (Response) obj;
                        mutex2.unlock(null);
                        return response;
                    }
                    mutex2 = mutex;
                    response = null;
                    mutex2.unlock(null);
                    return response;
                }
            }
            bluetoothGattCharacteristic = baseBleCharacteristic.rawCharacteristic;
            if (bluetoothGattCharacteristic == null) {
            }
        } catch (Throwable th3) {
//            mutex2 = mutex;
            th = th3;
            if (mutex != null) {
                mutex.unlock(null);
            }
//            throw th;
        }
        baseBleCharacteristic$readSynced$1 = new BaseBleCharacteristic$readSynced$1(this, (Continuation<? super BaseBleCharacteristic$readSynced$1>) continuation);
        Object obj2 = baseBleCharacteristic$readSynced$1.result;
        Object coroutine_suspended2 = IntrinsicsKt.getCOROUTINE_SUSPENDED();
        i = baseBleCharacteristic$readSynced$1.label;
        if (i != 0) {
        }
        return obj2;
    }

    public final void onCharacteristicValueRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int i) {
        Intrinsics.checkNotNullParameter(gatt, "gatt");
        Intrinsics.checkNotNullParameter(characteristic, "characteristic");
        onCharacteristicValueChange(gatt, characteristic);
    }

    public final void onCharacteristicValueChange(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        Intrinsics.checkNotNullParameter(gatt, "gatt");
        Intrinsics.checkNotNullParameter(characteristic, "characteristic");
        gattCallback(gatt, characteristic);
    }

    private final void gattCallback(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" Characteristic value change or read ");
        BluetoothGattCharacteristic bluetoothGattCharacteristic2 = this.rawCharacteristic;
        sb.append(bluetoothGattCharacteristic2 != null ? bluetoothGattCharacteristic2.getUuid() : null);
        sb.append(" value: ");
        byte[] value = bluetoothGattCharacteristic.getValue();
//        Intrinsics.checkNotNullExpressionValue(value, "characteristic.value");
        sb.append(ByteArrayExtKt.toHexString(value, true));
        log(sb.toString());
        try {
            Continuation<? super Response> continuation = this.responseContinuation;
            if (continuation != null) {
                Response createResponse = Commands.createResponse(bluetoothGattCharacteristic.getValue());
                Result.Companion companion = Result.Companion;
//                continuation.resumeWith(Result.constructor(createResponse));
                this.responseContinuation = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            onValueUpdate(bluetoothGatt, bluetoothGattCharacteristic);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        try {
            Function2<? super BluetoothGatt, ? super BluetoothGattCharacteristic, Unit> function2 = this.valueUpdateCallback;
            if (function2 != null) {
                function2.invoke(bluetoothGatt, bluetoothGattCharacteristic);
            }
        } catch (Exception e3) {
            e3.printStackTrace();
        }
    }

    public final void log(String content) {
        Intrinsics.checkNotNullParameter(content, "content");
        this.bleService.log(content);
    }

    public static final class Companion {
        public Companion(DefaultConstructorMarker defaultConstructorMarker) {
            this();
        }

        private Companion() {
        }

        public static BaseBleCharacteristic from$default(Companion companion, UUID uuid, BaseBleService baseBleService, Function2 function2, int i, Object obj) {
            if ((i & 4) != 0) {
                function2 = new Function2<BluetoothGatt, BluetoothGattCharacteristic, Unit>() {
                    public void invoke2(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
//                        Intrinsics.checkNotNullParameter(bluetoothGatt, "<anonymous parameter 0>");
//                        Intrinsics.checkNotNullParameter(bluetoothGattCharacteristic, "<anonymous parameter 1>");
                    }

                    @Override
                    public Unit invoke(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
                        invoke2(bluetoothGatt, bluetoothGattCharacteristic);
                        return Unit.INSTANCE;
                    }
                };
            }
            return companion.from(uuid, baseBleService, function2);
        }

        public BaseBleCharacteristic from(final UUID targetUUid, final BaseBleService bleService, final Function2<? super BluetoothGatt, ? super BluetoothGattCharacteristic, Unit> valueUpdateCallback) {
//            Intrinsics.checkNotNullParameter(targetUUid, "targetUUid");
//            Intrinsics.checkNotNullParameter(bleService, "bleService");
//            Intrinsics.checkNotNullParameter(valueUpdateCallback, "valueUpdateCallback");
            return new BaseBleCharacteristic(bleService) {
                @Override
                public UUID targetUUID() {
                    return targetUUid;
                }

                @Override
                public void onValueUpdate(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
//                    Intrinsics.checkNotNullParameter(gatt, "gatt");
//                    Intrinsics.checkNotNullParameter(characteristic, "characteristic");
                    valueUpdateCallback.invoke(gatt, characteristic);
                }
            };
        }
    }
}
